package com.ideal.frame.institutions.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ideal.frame.institutions.dto.InstitutionsServindustryDto;
import com.ideal.frame.institutions.entity.InstitutionsServindustryTb;
import com.ideal.frame.institutions.entity.QInstitutionsServindustryTb;
import com.ideal.frame.jpa.DSLRequest;
import com.ideal.frame.jpa.JpaRepositoryImpl;
import com.ideal.frame.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class InstitutionsServindustryService {
    @PersistenceContext
    protected EntityManager entityManager;
    private JpaRepositoryImpl<InstitutionsServindustryTb, String> jpaRepository;

    QInstitutionsServindustryTb qInstitutionsServindustryTb = QInstitutionsServindustryTb.institutionsServindustryTb;


    @PostConstruct
    public void initRepo() {
        jpaRepository = new JpaRepositoryImpl<>(InstitutionsServindustryTb.class, entityManager);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(InstitutionsServindustryDto servindustryDto) {
        Assert.isTrue(servindustryDto != null, "参数不能为空");
        String id = servindustryDto.getGnrlid();
        Assert.isTrue(StrUtil.isNotBlank(id), "机构id不能为空");
        DSLRequest request = new DSLRequest();
        request.addWhere(qInstitutionsServindustryTb.gnrlid.eq(id));
        request.addWhere(qInstitutionsServindustryTb.delFlag.isFalse());
        List<InstitutionsServindustryTb> all = jpaRepository.findAll(request);
        InstitutionsServindustryTb servindustryTb = new InstitutionsServindustryTb();
        BeanUtil.copyProperties(servindustryDto, servindustryTb);
        if (servindustryDto.getId() != null) {
            if (CollUtil.isNotEmpty(all)) {
                long count = all.stream().filter(v -> ObjectUtil.equal(v.getGnrlid(), servindustryDto.getGnrlid())).count();
                Assert.isTrue(count > 0, "机构id重复，请重新填写！");
            }
            servindustryTb.setModifyTime(new Date());
            servindustryTb.setModifyUser(UserUtils.getCurrentUser().getId());
        } else {
            Assert.isTrue(CollUtil.isEmpty(all), "机构id重复，请重新填写！");
            servindustryTb.setCreateTime(new Date());
            servindustryTb.setCreateUser(UserUtils.getCurrentUser().getId());
            servindustryTb.setDelFlag(false);
        }
        jpaRepository.save(servindustryTb);
    }

    public InstitutionsServindustryDto getByGnrlid(String gnrlid) {
        Optional<InstitutionsServindustryTb> tb = jpaRepository.findOne(qInstitutionsServindustryTb.gnrlid.eq(gnrlid)
                .and(qInstitutionsServindustryTb.delFlag.isFalse()));
        InstitutionsServindustryDto servindustryDto = new InstitutionsServindustryDto();
        tb.ifPresent(v -> BeanUtil.copyProperties(v, servindustryDto));
        return servindustryDto;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String gnrlid) {
        jpaRepository.update(qInstitutionsServindustryTb).set(qInstitutionsServindustryTb.delFlag, true)
                .where(qInstitutionsServindustryTb.gnrlid.eq(gnrlid)).execute();
    }
}
